package neu.soft.controller;

import neu.soft.constant.InvoiceType;
import neu.soft.entity.Invoice;
import neu.soft.entity.InvoiceGoodInfo;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.facade.MessageSender;
import neu.soft.query.InvoiceQuery;
import neu.soft.service.InvoiceService;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author 李博
 * 发票服务接口
 */
@RestController
@CrossOrigin
@RequestMapping("/invoice")
public class InvoiceController {

    @Autowired
    InvoiceService invoiceService;

    @Autowired
    MessageSender messageSender;

    //发票生成登记，返回发票,包含发票领用
    ////map里包含loc地址信息、batch批次信息，orderId，发票绑定的单号
    @PostMapping("/add")
    public ResultBean<Invoice> add(@RequestBody Map<String, Object> map) {
        String orderId = (String) map.get("orderId");//获取订单id/或购货单id
        boolean booleanRes = invoiceService.findInvoiceOrderId(orderId);
        if (booleanRes){
            throw new AppException(AppExceptionCodeMsg.REPEAT_FAIL);
        }
        String invoiceType = InvoiceType.INVOICING.getCode();
        map.put("invoiceType", invoiceType);
        Invoice invoice = invoiceService.buildInvoice(map);//生成发票
         List<InvoiceGoodInfo> invoiceGoodInfoList = invoice.getInvoiceGoodInfoList();
        System.out.println(invoiceGoodInfoList);
        invoiceGoodInfoList.forEach(invoiceGoodInfo -> invoiceGoodInfo.setInvoiceId(invoice.getInvoiceId()));
        invoiceService.addInvoice(invoice);//登记生成的发票
        invoiceService.insertInfoOrder(invoice.getInvoiceId(), orderId);//绑定财务中心购货单、订单和发票
        invoiceService.batchInsertGoodInfo(invoiceGoodInfoList);//批量插入发票商品列表
        return ResultBean.success(invoice);
    }

    //发票领用
    @PostMapping("/get")
    public ResultBean<Invoice> get(@RequestBody Map<String,Object> map) {
        String orderId = (String) map.get("orderId");
        Long invoiceId = invoiceService.findInvoiceByOrderId(orderId);//通过关联查询发票id，应付单里 的购货单id
        Invoice invoice = invoiceService.findById(invoiceId);//查询发票
        return ResultBean.success("发票领取成功", invoice);
    }

    //供应商发票登记
    @PostMapping("/register")
    public ResultBean register(@RequestBody Map<String, Object> map) {

        Invoice invoice = (Invoice) map.get("invoice");//得到发票
        String orderId = (String) map.get("orderId");//得到购货单id
        Integer operationId = Integer.valueOf((String) map.get("operationId"));
        String invoiceType = InvoiceType.LOSSES.getCode();//设置发票类型为挂账
        invoice.setInvoiceType(invoiceType);
        List<InvoiceGoodInfo> invoiceGoodInfoList = invoice.getInvoiceGoodInfoList();
        invoiceService.addInvoice(invoice);
        invoiceService.batchInsertGoodInfo(invoiceGoodInfoList);
        invoiceService.insertInfoOrder(invoice.getInvoiceId(), orderId);
        boolean res = messageSender.sendInfo(operationId);
        if (!res) {
            throw new AppException(AppExceptionCodeMsg.SEND_FAIL);
        }
        return ResultBean.success();
    }

    @PostMapping("/page")
    public ResultBean findByPage(@RequestBody InvoiceQuery invoiceQuery){
        return ResultBean.success(invoiceService.findByPage(invoiceQuery));
    }


}
